package de.saarland.uni.lego3d.server.gui.handler;

import java.awt.Cursor;
import java.awt.Point;

import javax.swing.ImageIcon;

import de.saarland.uni.lego3d.server.gui.MainGUI;
import de.saarland.uni.lego3d.server.gui.components.Java3DPanel;

public class KinectActionCursor extends Thread {
	
	/**
	 * Reference to the mainGUI to be able to call actions on it
	 */
	private MainGUI mGui;
	
	/**
	 * Reference to the Java3D Panel to be able to change cursor layout on it
	 */
	private Java3DPanel parent;
	
	/**
	 * Indicating the mode of this Handler
	 * 0 - Java3DPanel handler
	 * 1 - HistoryPanel handler
	 * 2 - TempModelPanel handler
	 */
	private int mode = -1;
	
	/**
	 * Time that decreases in the WAIT_TIME interval until 0, then the according action of mode is called in the mainGUI
	 */
	private int time = 5;
	
	/**
	 * Interval of decreasing the time value
	 */
	private final int WAIT_TIME = 500;
	
	Cursor c1;
	Cursor c2;
	Cursor c3;
	Cursor c4;
	Cursor c5;
	
	public KinectActionCursor(Java3DPanel parent, MainGUI mainFrame, int mode) {
		mGui = mainFrame;
		c1 = mGui.getToolkit().createCustomCursor(new ImageIcon( this.getClass().getResource("/de/saarland/uni/lego3d/textures/loadc_1.png")).getImage(), new Point(10,10), "Cursor" );
		c2 = mGui.getToolkit().createCustomCursor(new ImageIcon( this.getClass().getResource("/de/saarland/uni/lego3d/textures/loadc_2.png")).getImage(), new Point(10,10), "Cursor" );
		c3 = mGui.getToolkit().createCustomCursor(new ImageIcon( this.getClass().getResource("/de/saarland/uni/lego3d/textures/loadc_3.png")).getImage(), new Point(10,10), "Cursor" );
		c4 = mGui.getToolkit().createCustomCursor(new ImageIcon( this.getClass().getResource("/de/saarland/uni/lego3d/textures/loadc_4.png")).getImage(), new Point(10,10), "Cursor" );
		c5 = mGui.getToolkit().createCustomCursor(new ImageIcon( this.getClass().getResource("/de/saarland/uni/lego3d/textures/loadc_5.png")).getImage(), new Point(10,10), "Cursor" );
		this.parent = parent;
		this.mode = mode;
	}
	
	@Override
	public void run() {
		while (!isInterrupted() && time >= 0) {
			if (time == 5) {
				parent.setCursor(c5);
			} else if (time == 4) {
				parent.setCursor(c4);
			} else if (time == 3) {
				parent.setCursor(c3);
			} else if (time == 2) {
				parent.setCursor(c2);
			} else if (time == 1) {
				parent.setCursor(c1);
			} else if (time == 0) {
				parent.setCursor(Cursor.getDefaultCursor());
				
				if (mode == 1)
					mGui.switchPanels(parent);
				else if (mode == 2)
					mGui.addTmpModel(parent.getImage());
				else if (mode == 3)
					mGui.addAndSaveModel(parent.getImage());
					
				interrupt();
				continue;
			}
			try {
				time--;
				sleep(WAIT_TIME);
			} catch (InterruptedException e) {
				interrupt();
			}
		}
	}
}
